import {
  Module,
  NestModule,
  MiddlewareConsumer,
  RequestMethod
} from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { HttpErrorFilter } from './common/filters/http-exception.filter';
import { LoggingInterceptor } from './common/interceptors/logging.interceptor';
import { TestModule } from './test/test.module';
import { AuthModule } from './auth/auth.module';
import { MockUrlMiddleware } from './common/middlewares/mock-url.middleware';
import { ProxyModule } from './proxy/proxy.module';
import { UserModule } from './user/user.module';
import { DatabaseModule } from './database/database.module';
import { DemandModule } from './demand/demand.module';
import { TeamModule } from './team/team.module';

import config from './config/config';
import { InterfaceModule } from './interface/interface.module';

@Module({
  imports: [
    MongooseModule.forRoot(config.mongodb.url, { useNewUrlParser: true }),
    DatabaseModule,
    AuthModule,
    UserModule,
    DemandModule,
    TeamModule,
    InterfaceModule,
    TestModule,
    // ProxyModule要保证最后一个引入
    ProxyModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_FILTER,
      useClass: HttpErrorFilter
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor
    }
  ]
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(MockUrlMiddleware).forRoutes({
      path: '*',
      method: RequestMethod.ALL
    });
  }
}
